import { IMapSource } from './mapTypes';

/**
 * 加载 本地/服务器某地 png 图片作为底图
 */
export const LocalPngTilesConfig: IMapSource = {
  style: {
    version: 8,
    sources: {
      'raster-tiles': {
        type: 'raster',
        tiles: [`../../public/map/yinchuan/{z}/{x}/{y}.png`],
        tileSize: 256,
        // 添加缓存配置，显式启用
        cache: true,
        // 设置缓存参数，避免token参数影响缓存
        cacheControl: 'max-age=86400', // 缓存24小时
        // 设置最大缓存数量
        maxzoom: 18,
        minzoom: 0,
      },
    },
    layers: [
      {
        id: 'yinchuan',
        type: 'raster',
        source: 'raster-tiles',
      },
    ],
    glyphs: 'fonts/{fontstack}/{range}.pbf',
  },
  onLoad() {
    console.log('local png map init done');
  },
};

export const GetLocalPngTilesConfig = (params: { tilesPath: string }) => {
  params = params || {
    tilesPathPrefix: '../../public/map/yinchuan',
    tilesPathSuffix: '',
  };
  // 确保 style 是对象而不是字符串或undefined
  if (
    typeof LocalPngTilesConfig.style === 'object' &&
    LocalPngTilesConfig.style &&
    LocalPngTilesConfig.style.sources
  ) {
    const rasterSource = LocalPngTilesConfig.style.sources[
      'raster-tiles'
    ] as any;
    // rasterSource.tiles = [`${params.tilesPathPrefix}/{z}/{x}/{y}.png${params.tilesPathSuffix}`];
    rasterSource.tiles = [params.tilesPath];
  }
  return LocalPngTilesConfig;
};
